/*
 * Copyright 2008-2009 Daniel Freitas
 * 
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Army Creator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Army Creator. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package com.googlecode.armycreator.datastore.test.integration

import com.googlecode.armycreator.datastore.genericdao.GenericDao
import com.googlecode.armycreator.datastore.test.PersistentDummy
import java.sql.Connection
import java.sql.Statement
import javax.sql.DataSource
import org.springframework.context.ApplicationContext
import org.testng.ITestContext
import org.testng.annotations.BeforeClass
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test

public class HibernateGenericDaoIntegrationTest {
    private GenericDao<PersistentDummy> hibernateGenericDao
    private ApplicationContext applicationContext

    @BeforeClass (alwaysRun = true)
    public void setUp(ITestContext testContext) throws Exception {
        applicationContext = (ApplicationContext) testContext.getAttribute("applicationContext")
        hibernateGenericDao = (GenericDao<PersistentDummy>) applicationContext.getBean("persistentDummyHibernateGenericDao")
    }

    @BeforeMethod (alwaysRun = true)
    public void populateData() throws Exception {
        DataSource dataSource = (DataSource) applicationContext.getBean("dataSource")
        Connection con = dataSource.getConnection()
        Statement statement = con.createStatement()
        URL filePath = getClass().getResource("/com/googlecode/armycreator/test-db-data.sql")
        statement.execute("RUNSCRIPT FROM '" + filePath + "'")
        statement.close()
        con.close()
    }

    @Test (groups = ["integration", "fast"])
    public void testMakePersistent() throws MalformedURLException {
        PersistentDummy persistentDummy = new PersistentDummy("Daniel", "Freitas", 30)

        PersistentDummy persistedInstance = hibernateGenericDao.findById(hibernateGenericDao.makePersistent(
                persistentDummy).getId())

        assert persistedInstance != persistentDummy
        assert persistedInstance.getId().equals(persistentDummy.getId())
    }

    @Test (groups = ["integration", "fast"])
    public void testFindAll() {
        List<PersistentDummy> all = hibernateGenericDao.findAll()

        assert all.size() == 3
    }

    @Test (groups = ["integration", "fast"])
    public void testFindByExample() {
        PersistentDummy persistentDummy = new PersistentDummy("Daniel", "Freitas", 30)

        List<PersistentDummy> matches = hibernateGenericDao.findByExample(persistentDummy, "uuid")
        assert matches.size() == 1
    }

    @Test (groups = ["integration", "fast"])
    public void testFindById() {
        PersistentDummy persistentDummy = hibernateGenericDao.findById(1L)

        assert persistentDummy.getId() == 1L
    }

    @Test (groups = ["integration", "fast"])
    public void testMakeTransient() {
        PersistentDummy persistentDummy = new PersistentDummy("Daniel", "Freitas", 30)

        PersistentDummy persistedInstance = hibernateGenericDao.findById(hibernateGenericDao.makePersistent(
                persistentDummy).getId())
        Long persistedInstanceId = persistedInstance.getId()
        hibernateGenericDao.makeTransient(persistedInstance)

        PersistentDummy nullInstance = hibernateGenericDao.findById(persistedInstanceId)

        assert nullInstance == null
        assert persistedInstance.getId() == null
    }

    @Test (groups = ["integration", "fast"])
    public void testFindByIdReturnsNullIfIdNotFound() {
        PersistentDummy hasToBeNull = hibernateGenericDao.findById(-25)

        assert hasToBeNull == null
    }
}
